
package com.zdkj.subway.stm.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zdkj.common.security.util.ObjectUtils;
import com.zdkj.subway.stm.dto.in.SwStInternetTicketDataDTO;
import com.zdkj.subway.stm.entity.SwStInternetTicketEquipment;
import com.zdkj.subway.stm.entity.SwStInternetTicketEquipmentDetails;
import com.zdkj.subway.stm.mapper.SwStInternetTicketEquipmentMapper;
import com.zdkj.subway.stm.service.SwStInternetTicketEquipmentDetailsService;
import com.zdkj.subway.stm.service.SwStInternetTicketEquipmentService;
import com.zdkj.subway.stm.util.BeanUtilsStm;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 互联网售票机主表
 *
 * @author songtuo
 * @date 2020-03-07 20:14:22
 */
@Service
public class SwStInternetTicketEquipmentServiceImpl extends ServiceImpl<SwStInternetTicketEquipmentMapper, SwStInternetTicketEquipment> implements SwStInternetTicketEquipmentService {

    @Autowired
    SwStInternetTicketEquipmentDetailsService detailsService;

    @Autowired
    SwStInternetTicketEquipmentMapper swStInternetTicketEquipmentMapper;

    @Override
    @Transactional
    public boolean saveSwStInternetTicketData(SwStInternetTicketDataDTO swStInternetTicketDataDto) {
        //SwStInternetTicketEquipment swStInternetTicketEquipment = swStInternetTicketDataDto.getSwStInternetTicketEquipment();
        //公共属性复制
        //SwStInternetTicketEquipment swStTvmCheckDetailObj = ObjectUtils.getDefaultObj(SwStInternetTicketEquipment.class);
        //BeanUtil.copyProperties(swStTvmCheckDetailObj, swStInternetTicketEquipment, CopyOptions.create().setIgnoreNullValue(true));
        List<SwStInternetTicketEquipmentDetails> swStInternetTicketEquipmentDetails = swStInternetTicketDataDto.getSwStInternetTicketEquipmentDetails();
        if (CollectionUtil.isEmpty(swStInternetTicketEquipmentDetails)) {
            return false;
        }
        //countNumberAndAmount(swStInternetTicketEquipment, swStInternetTicketEquipmentDetails);
        //save(swStInternetTicketEquipment);
        //保存从表
        BeanUtilsStm.buildProperties(swStInternetTicketEquipmentDetails);
        if(CollectionUtil.isNotEmpty(swStInternetTicketEquipmentDetails)){
            //swStInternetTicketEquipmentDetails.forEach(sw -> sw.setRelationId(swStInternetTicketEquipment.getId()));
            swStInternetTicketEquipmentDetails.forEach(sw -> sw.setUnitPrice(sw.getTicketAmount()));
            detailsService.saveBatch(swStInternetTicketEquipmentDetails);
        }
        return true;
    }

    /**
     * 统计总额度
     *
     * @param swStInternetTicketData
     * @param swStInternetTicketDataDetails
     */
    private void countNumberAndAmount(SwStInternetTicketEquipment swStInternetTicketData, List<SwStInternetTicketEquipmentDetails> swStInternetTicketDataDetails) {
        if (CollectionUtil.isNotEmpty(swStInternetTicketDataDetails)) {
            Double totalAmount = swStInternetTicketDataDetails.stream().map(sw -> sw.getTotalSaleAmount() == null ? 0d : sw.getTotalSaleAmount().doubleValue())
                    .reduce(Double::sum).orElse(0D);
            swStInternetTicketData.setSubtotalTotal(new BigDecimal(totalAmount));
            Integer totalSaleNumber = swStInternetTicketDataDetails.stream().map(sw -> sw.getSaleNumber() == null ? 0 : sw.getSaleNumber()).reduce(Integer::sum).orElse(0);
            swStInternetTicketData.setTotalNumberSales(totalSaleNumber);
        }
    }

    /**
     * 更新主表和从表  需要注意的是  从表可能有增删改的场景 需要注意
     * @param swStInternetTicketDataDto
     * @return
     */
    @Override
    @Transactional
    public boolean updateSwStInternetTicketData(SwStInternetTicketDataDTO swStInternetTicketDataDto) {
        List<SwStInternetTicketEquipmentDetails> swStInternetTicketEquipmentDetails = swStInternetTicketDataDto.getSwStInternetTicketEquipmentDetails();
        SwStInternetTicketEquipment swStInternetTicketEquipment = swStInternetTicketDataDto.getSwStInternetTicketEquipment();
        Integer rid = swStInternetTicketEquipment.getId();
        swStInternetTicketEquipmentDetails.forEach(x -> x.setRelationId(rid));
        swStInternetTicketEquipmentDetails.forEach(x -> x.setUnitPrice(x.getTicketAmount()));
        List<Integer> ids = swStInternetTicketEquipmentDetails.stream().map(SwStInternetTicketEquipmentDetails::getId).collect(Collectors.toList());
        List<SwStInternetTicketEquipmentDetails> list = detailsService.list(new QueryWrapper<SwStInternetTicketEquipmentDetails>().lambda().eq(SwStInternetTicketEquipmentDetails::getRelationId, rid));
        List<Integer> orginIds = list.stream().map(SwStInternetTicketEquipmentDetails::getId).collect(Collectors.toList());
        Collection<Integer> removeIds = CollectionUtils.subtract(orginIds, ids);
        detailsService.saveOrUpdateBatch(swStInternetTicketEquipmentDetails);
        if (CollectionUtils.isNotEmpty(removeIds)) {
            detailsService.removeByIds(removeIds);
        }
        countNumberAndAmount(swStInternetTicketEquipment, swStInternetTicketEquipmentDetails);
        updateById(swStInternetTicketEquipment);
        return true;
    }

    /**
     * 查询详情 通过id
     * @param id
     * @return
     */
    @Override
    public SwStInternetTicketDataDTO queryDetailsByid(Integer id) {
        SwStInternetTicketDataDTO swStInternetTicketDataDTO = new SwStInternetTicketDataDTO();
        SwStInternetTicketEquipment swStInternetTicketEquipment = getById(id);
        List<SwStInternetTicketEquipmentDetails> details = detailsService.list(new QueryWrapper<SwStInternetTicketEquipmentDetails>().lambda().eq(SwStInternetTicketEquipmentDetails::getRelationId, id));
        swStInternetTicketDataDTO.setSwStInternetTicketEquipmentDetails(new ArrayList<>(details));
        swStInternetTicketDataDTO.setSwStInternetTicketEquipment(swStInternetTicketEquipment);
        return swStInternetTicketDataDTO;
    }

    @Override
    @Transactional
    public boolean deleteCascadeById(Integer id) {
        removeById(id);
        //查询从表
        detailsService.remove(new QueryWrapper<SwStInternetTicketEquipmentDetails>().lambda().eq(SwStInternetTicketEquipmentDetails::getRelationId, id));
        return true;
    }

    @Override
    public boolean updateApprovalStatus(List<Integer> ids, String status) {
        return swStInternetTicketEquipmentMapper.updateApprovalStatus(ids,status);
    }



}
